package com.zdb;

import org.apache.hc.client5.http.classic.methods.HttpGet;
import org.apache.hc.client5.http.classic.methods.HttpPost;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.ClassicHttpResponse;
import org.apache.hc.core5.http.ContentType;
import org.apache.hc.core5.http.ParseException;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.hc.core5.http.io.entity.StringEntity;

import java.io.IOException;

public class Demo1 {

    // 替换为你的 API Key
    private static final String API_KEY = "18c64c7b16d869f86953ade9571c3b87.FVhAx2TS7X7JB04n";

    // 智谱 AI 知识库接口地址
    private static final String ENDPOINT = "https://open.bigmodel.cn/api/llm-application/open/knowledge/1940334241741254656";

    public static void main(String[] args) {
        try (CloseableHttpClient httpClient = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(ENDPOINT);

            // 设置请求头
            request.setHeader("accept", "*/*");
            request.setHeader("Content-Type", "application/json");
            request.setHeader("Authorization", "Bearer " + API_KEY);

            // 构造请求体（JSON 格式）
            String jsonBody = "{"
                    + "\"embedding_id\": 1,"
                    + "\"name\": \"测试知识库\","
                    + "\"description\": \"用于测试\""
                    + "}";

            // 设置请求体
            request.setEntity(new StringEntity(jsonBody, ContentType.APPLICATION_JSON));

            // 发送请求并获取响应
            ClassicHttpResponse response = httpClient.execute(request);
            int statusCode = response.getCode();
            System.out.println("Status Code: " + statusCode);

            if (response.getEntity() != null) {
                String result = EntityUtils.toString(response.getEntity());
                System.out.println("Response Body: " + result);
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }
}
